package com.modules.sys.controller;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.common.annotation.SysLog;
import com.common.utils.PageUtils;
import com.common.utils.Query;
import com.common.utils.R;
import com.common.validator.Assert;
import com.common.validator.ValidatorUtils;
import com.common.validator.group.AddGroup;
import com.common.validator.group.UpdateGroup;
import com.modules.sys.entity.SysUserEntity;
import com.modules.sys.service.SysUserRoleService;
import com.modules.sys.service.SysUserService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 系统用户
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年10月31日 上午10:40:10
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController extends AbstractController {
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysUserRoleService sysUserRoleService;
	
	/**
	 * 所有用户列表
	 */
	@RequestMapping("/list")
//	@RequiresPermissions("sys:user:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<SysUserEntity> userList = sysUserService.queryList(query);
		int total = sysUserService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(userList, total, query.getLimit(), query.getPage());
		
		return R.success().put("page", pageUtil);
	}
	
	/**
	 * 获取登录的用户信息
	 */
	@RequestMapping("/info")
	public R info(){
		return R.ok().put("user", getUser());
	}
	
	/**
	 * 修改登录用户密码
	 */
	@SysLog("修改密码")
	@RequestMapping("/password")
	public R password(String password, String newPassword){
		Assert.isBlank(newPassword, "新密码不为能空");

		//sha256加密
		password = new Sha256Hash(password, getUser().getSalt()).toHex();
		//sha256加密
		newPassword = new Sha256Hash(newPassword, getUser().getSalt()).toHex();
				
		//更新密码
		int count = sysUserService.updatePassword(getUserId(), password, newPassword);
		if(count == 0){
			return R.error("原密码不正确");
		}
		
		return R.ok();
	}
	
	/**
	 * 用户信息
	 */
	@RequestMapping("/info/{userId}")
//	@RequiresPermissions("sys:user:info")
	public R info(@PathVariable("userId") Long userId){
		SysUserEntity user = sysUserService.queryObject(userId);
		
		//获取用户所属的角色列表
		List<Long> roleIdList = sysUserRoleService.queryRoleIdList(userId);
		user.setRoleIdList(roleIdList);
		
		return R.ok().put("user", user);
	}
	
//	/**
//	 * 保存用户
//	 */
//	@SysLog("保存用户")
//	@RequestMapping("/save")
////	@RequiresPermissions("sys:user:save")
//	public R save(@RequestBody SysUserEntity user){
//		//ValidatorUtils.validateEntity(user, AddGroup.class);
//		
//		user.setCreateUserId(getUserId());
//		user.setDeptId(1L);
//		user.setEmail("xx@xx.com");
//		sysUserService.save(user);
//		
//		return R.ok();
//	}
	
	@RequestMapping("/save")
	public R save(String username, String mobile, String password) {
		List<Long> roleIdList = new ArrayList<Long>();
		roleIdList.add(1L);
		SysUserEntity user = new SysUserEntity();
//		user.setCreateUserId(getUserId());
		user.setUsername(username);
		user.setMobile(mobile);
		user.setPassword(password);
		user.setDeptId(1L);
		user.setEmail("xx@xx.com");
		user.setRoleIdList(roleIdList);
		user.setStatus(1);
		sysUserService.save(user);
		
		return R.success();
	}
	
//	/**
//	 * 修改用户
//	 */
//	@SysLog("修改用户")
//	@RequestMapping("/update")
////	@RequiresPermissions("sys:user:update")
//	public R update(@RequestBody SysUserEntity user){
//		//ValidatorUtils.validateEntity(user, UpdateGroup.class);
//		
//		user.setCreateUserId(getUserId());
//		user.setDeptId(1L);
//		user.setEmail("xx@xx.com");
//		sysUserService.update(user);
//		
//		return R.ok();
//	}
	
	@RequestMapping("/update")
	public R update(Long userId, String username, String mobile, String oldPassword, String newPassword) {
		SysUserEntity user = sysUserService.queryObject(userId);
		if(StringUtils.isBlank(oldPassword)) {
			return R.error("旧密码不能为空");
		}
		if(!user.getPassword().equals(new Sha256Hash(oldPassword, user.getSalt()).toHex())) {
			return R.error("旧密码不正确，请重新输入");
		}
		user.setUsername(username);
		user.setMobile(mobile);
		user.setPassword(new Sha256Hash(newPassword, user.getSalt()).toHex());
		sysUserService.update(user);
		
		return R.success();
	}
	
//	/**
//	 * 删除用户
//	 */
//	@SysLog("删除用户")
//	@RequestMapping("/delete")
////	@RequiresPermissions("sys:user:delete")
//	public R delete(@RequestBody Long[] userIds){
//		if(ArrayUtils.contains(userIds, 1L)){
//			return R.error("系统管理员不能删除");
//		}
//		
//		if(ArrayUtils.contains(userIds, getUserId())){
//			return R.error("当前用户不能删除");
//		}
//		
//		sysUserService.deleteBatch(userIds);
//		
//		return R.ok();
//	}
	
	@RequestMapping("/delete")
	public R delete(Long userId) {
		if(userId.longValue() == 1L) {
			return R.error("系统管理员不能删除");
		}
		
		Long[] userIds = new Long[] {userId};
		sysUserService.deleteBatch(userIds);
		
		return R.success();
	}
}

